var Temperature = {
		actualTemperatures : 
        {
            bedroom1: "",
            bedroom2: "",
            livingroom: "",
            lobby: "",
            bathroom: ""
        },
        
        wantedTemperatures : 
        {
        	bedroom1: "",
        	bedroom2: "",
        	livingroom: "",
        	lobby: "",
        	bathroom: ""
        },
		
		getTemperatures : function() {
			Temperature.getActualTemperatures();
		},
		
		getActualTemperatures : function() {
			var uri =  "/gosmart/temperature?actualTemperatures";
			$.ajax({
				type: 'GET',
				url: uri,
				dataType: 'json',
				beforeSend: function(xhr)
				{
					$("#loading").css ("display", "block");
					return true;
				},
				success: function(response) 
				{ 
					$("#loading").css ("display", "none");
					Temperature.actualTemperatures = response.temperatures;
					Temperature.getWantedTemperatures();
				},
				error: function(xhr, reason, exception) 
				{             	
					alert(xhr);
					alert(reason);
					alert(exception);
				}
			});
			return false; 
		},
		
		getWantedTemperatures : function() {
			var uri =  "/gosmart/temperature?wantedTemperatures";
			$.ajax({
				type: 'GET',
				url: uri,
				dataType: 'json',
				beforeSend: function(xhr)
				{
					$("#loading").css ("display", "block");
					return true;
				},
				success: function(response) 
				{ 
					$("#loading").css ("display", "none");
					Temperature.wantedTemperatures = response.temperatures;
					Temperature.renderTemperatures();
				},
				error: function(xhr, reason, exception) 
				{             	
					Temperature.renderError(reason);
				}
			});
			return false; 
		},
		
		submitTemperatureThreshold : function (room) {
			
			var tmp = "new" + room + "Temp";
			var tempThr = document.getElementById("new" + room + "Temp").value;
			var uri =  "/gosmart/temperature?wantedTemperature=" + tempThr + "&room=" + room.toLowerCase();
			$.ajax({
				type: 'POST',
				url: uri,
				dataType: 'json',
				error: function(xhr, reason, exception) 
				{             	
					Temperature.renderError(reason);
				},
				success: function(response) {
					Temperature.renderSuccess(response);
				},
				
				complete: function () 
				{
					Temperature.getTemperatures();
				}
			});
		},
		
		renderTemperatures : function () {
			$("#content").html("");
			var html = "";
			html += "<table>";
			html += "<thead>";
			html += "<tr><th>Room</th><th>Actual Temp</th><th>Wanted Temp</th><th>New Wanted Temp</th><th></th></tr>";
			html += "</thead>";
			html += "<tbody>";
			html += "<tr><td>Bedroom1</td><td>" + Temperature.actualTemperatures.bedroom1 + "</td><td>" + Temperature.wantedTemperatures.bedroom1 + "</td><td><input type=\"text\" id=\"newBedroom1Temp\" size=\"8\" /></td><td><button onClick='Temperature.submitTemperatureThreshold(\"Bedroom1\");'>Submit</button></td></tr>";
			html += "<tr><td>Bedroom2</td><td>" + Temperature.actualTemperatures.bedroom2 + "</td><td>" + Temperature.wantedTemperatures.bedroom2 + "</td><td><input type=\"text\" id=\"newBedroom2Temp\" size=\"8\" /></td><td><button onClick='Temperature.submitTemperatureThreshold(\"Bedroom2\");'>Submit</button></td></tr>";
			html += "<tr><td>Livinroom</td><td>" + Temperature.actualTemperatures.livingroom + "</td><td>" + Temperature.wantedTemperatures.livingroom + "</td><td><input type=\"text\" id=\"newLivingroomTemp\" size=\"8\" /></td><td><button onClick='Temperature.submitTemperatureThreshold(\"Livingroom\");'>Submit</button></td></tr>";
			html += "<tr><td>Lobby</td><td>" + Temperature.actualTemperatures.lobby + "</td><td>" + Temperature.wantedTemperatures.lobby + "</td><td><input type=\"text\" id=\"newLobbyTemp\" size=\"8\" /></td><td><button onClick='Temperature.submitTemperatureThreshold(\"Lobby\");'>Submit</button></td></tr>";
			html += "<tr><td>Bathroom</td><td>" + Temperature.actualTemperatures.bathroom + "</td><td>" + Temperature.wantedTemperatures.bathroom + "</td><td><input type=\"text\" id=\"newBathroomTemp\" size=\"8\" /></td><td><button onClick='Temperature.submitTemperatureThreshold(\"Bathroom\");'>Submit</button></td></tr>";
			html += "</tbody>"
			html += "</table>";
			$("#content").html(html);
		},
		
		renderError: function (msg) {
			$("#log").html(""); //get rid of any previous result
			$("#log").html("<p class='error'>"+msg+"</p>");
		},
		
		renderSuccess: function (msg) {
			$("#log").html(""); //get rid of any previous result
			$("#log").html("<p class='success'>"+msg+"</p>");
		}
	}

$(document).ready (function () {
	Temperature.getTemperatures();
}); 